/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exemplo2;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author Elise
 */
public class Exemplo2 extends JFrame {

    /**
     * @param args the command line arguments
     */
    JButton ok;
    JButton desfazer;
    JTextField texto;
    JLabel nome;
    Cachorro cachorro = new Cachorro("Labrador");
    Caretaker ct = new Caretaker();

    Exemplo2() {
        ok = new JButton("OK");
        desfazer = new JButton("Desfazer");
        texto = new JTextField();
        nome = new JLabel(cachorro.getNome());


        //Interface gráfica
        nome.setHorizontalAlignment(JLabel.CENTER);
        nome.setFont(new Font("Serif", Font.PLAIN, 18));
        texto.setFont(new Font("Serif", Font.PLAIN, 18));

        //Interface gráfica
        this.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
        this.add(nome, g);
        g = new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 30, 0, 0), 0, 0);
        this.add(texto, g);
        g = new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
        this.add(ok, g);
        g = new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
        this.add(desfazer, g);
        this.setSize(500, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Implementando memento aqui:
        ActionListener act = new ActionListener() {

            //Muda estado e guarda o anterior:
            @Override
            public void actionPerformed(ActionEvent e) {
                ct.inserirMemento(cachorro.createMemento());//cachorro cria memento com seu estado atual e o caretaker guarda esse memento
                cachorro.setNome(texto.getText());// atualiza para o novo estado
                nome.setText(cachorro.getNome());//mostra o novo nome cachorro
            }
        };
        ok.addActionListener(act);

        act = new ActionListener() {

            //Desfazer aqui:
            @Override
            public void actionPerformed(ActionEvent e) {
                Cachorro.Memento aux;
                aux = ct.removerMemento();
                if (aux != null) {                    
                    /*Volta para estado anterior*/
                    cachorro.setMemento(aux);
                    nome.setText(cachorro.getNome());
                }
            }
        };
        desfazer.addActionListener(act);

    }

    public static void main(String[] args) {
        Exemplo2 e = new Exemplo2();
        e.setVisible(true);
    }
}
